# cli/number_client.py

import sys
import rclpy
from rclpy.node import Node
from example_interfaces.srv import AddThreeInts

class NumberClient(Node):
    def __init__(self):
        super().__init__('number_client')
        self.cli = self.create_client(AddThreeInts, 'process_numbers')
        while not self.cli.wait_for_service(timeout_sec=1.0):
            self.get_logger().info('Service not available, waiting again...')
        self.req = AddThreeInts.Request()

    def send_request(self, a, b, c, operation):
        self.req.a = a
        self.req.b = b
        self.req.c = c
        self.req.operation = operation
        self.future = self.cli.call_async(self.req)

def main(args=None):
    rclpy.init(args=args)

    if len(sys.argv) != 5:
        print("Usage: number_client.py [num1] [num2] [num3] [operation(add|multiply)]")
        return

    a = float(sys.argv[1])
    b = float(sys.argv[2])
    c = float(sys.argv[3])
    operation = sys.argv[4]

    number_client = NumberClient()
    number_client.send_request(a, b, c, operation)

    while rclpy.ok():
        rclpy.spin_once(number_client)
        if number_client.future.done():
            try:
                response = number_client.future.result()
            except Exception as e:
                number_client.get_logger().info('Service call failed %r' % (e,))
            else:
                number_client.get_logger().info(f'Result: {response.sum}')
            break

    number_client.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
